package com.shangcheng.server.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangcheng.model.domain.OmsCompanyAddress;
import com.shangcheng.model.domain.OmsOrderReturnApply;
import com.shangcheng.model.dto.OmsOrderReturnApplyAndOmsCompanyAddress;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.server.mapper.OmsOrderReturnApplyMapper;
import com.shangcheng.server.service.OmsCompanyAddressService;
import com.shangcheng.server.service.OmsOrderReturnApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author JavaClimber
 * @version 1.0
 * @date 2022/5/14 0014 zihao
 */
@Service
public class OmsOrderReturnApplyServiceImpl extends ServiceImpl<OmsOrderReturnApplyMapper, OmsOrderReturnApply> implements OmsOrderReturnApplyService {
    @Autowired
    private OmsCompanyAddressService omsCompanyAddressService;
    
    //分页查询退货申请
    @Override
    public PageResult lists(Map<String, String> map, Integer pageNum, Integer pageSize) {
        //先把申请时间转成date
        String time = map.get("createTime");
        DateTime parse = DateUtil.parse(time);
        //再划定一个时间范围:用糊涂的起始时间工具
        Date beginOfDay = new Date();
        Date endOfDay = new Date();
        if (StringUtils.isNotEmpty(time)) {
            Date date = DateUtil.parse(time);
            beginOfDay = DateUtil.beginOfDay(date);
            endOfDay = DateUtil.endOfDay(date);
        }
        
        //提取所有的数据
        //处理人员(模糊)
        String handleMan = map.get("handleMan");
        //处理时间
        String createTime = map.get("handleTime");
        //服务单号(模糊)
        String id = map.get("id");
        //收货人姓名/号码(模糊)
        String receiverKeyword = map.get("receiverKeyword");
        //申请状态：0->待处理；1->退货中；2->已完成；3->已拒绝
        String status = map.get("status");
        //查询
        Page<OmsOrderReturnApply> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OmsOrderReturnApply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(StringUtils.isNotEmpty(time), OmsOrderReturnApply::getCreateTime, beginOfDay, endOfDay)
                .like(StringUtils.isNotEmpty(handleMan), OmsOrderReturnApply::getHandleMan, handleMan)
                .eq(StringUtils.isNotEmpty(createTime), OmsOrderReturnApply::getCreateTime, createTime)
                .eq(StringUtils.isNotEmpty(id), OmsOrderReturnApply::getId, id)
                .like(StringUtils.isNotEmpty(receiverKeyword), OmsOrderReturnApply::getReturnName, receiverKeyword)
                .or(StringUtils.isNotEmpty(receiverKeyword))
                .like(StringUtils.isNotEmpty(receiverKeyword), OmsOrderReturnApply::getReturnPhone, receiverKeyword)
                .eq(StringUtils.isNotEmpty(status), OmsOrderReturnApply::getStatus, status);
        //封装page
        Page<OmsOrderReturnApply> pp = this.page(page, lambdaQueryWrapper);
        PageResult pageResult = new PageResult(Convert.toInt(pp.getCurrent()), Convert.toInt(pp.getSize()), Convert.toInt(pp.getTotal()), pp.getRecords());
        return pageResult;
    }
    
    //修改退货申请状态
    @Override
    public boolean updateByIds(String id, OmsOrderReturnApply omsOrderReturnApply) {
        //查到数据
        OmsOrderReturnApply bb = this.getById(id);
        //先判断填写的退款金额是否大于数据库原价
        if (Convert.toDouble(omsOrderReturnApply.getReturnAmount()) > Convert.toDouble(bb.getProductRealPrice())) {
            return false;
        }
        
        //获取当前时间
        String now = DateUtil.now();
        //定义条件,按照id查询
        LambdaQueryWrapper<OmsOrderReturnApply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OmsOrderReturnApply::getId, id);
        
        //判断
        if (StringUtils.isEmpty(bb.getCreateTime())) {
            //如果查询到的create_time申请时间是空的,那么就要更新申请时间
            omsOrderReturnApply.setHandleTime(now);
        } else {
            //如果查询到不是空的,那么就要更新receive_time收货时间
            omsOrderReturnApply.setReceiveTime(now);
        }
        
        return this.update(omsOrderReturnApply, lambdaQueryWrapper);
    }
    
    //获取退货申请详情
    @Override
    public OmsOrderReturnApplyAndOmsCompanyAddress getByIds(Long id) {
        //根据id获取OmsOrderReturnApply订单退货申请表
        OmsOrderReturnApply omsOrderReturnApply = this.getById(id);
        //找到表内的CompanyAddressId收货地址表id
        Long companyAddressId = omsOrderReturnApply.getCompanyAddressId();
        //根据收货地址表id查询收获地址表
        OmsCompanyAddress omsCompanyAddress = omsCompanyAddressService.getById(companyAddressId);
        
        OmsOrderReturnApplyAndOmsCompanyAddress omsOrderReturnApplyAndOmsCompanyAddress = new OmsOrderReturnApplyAndOmsCompanyAddress();
        BeanUtils.copyProperties(omsOrderReturnApply, omsOrderReturnApplyAndOmsCompanyAddress);
        omsOrderReturnApplyAndOmsCompanyAddress.setOmsCompanyAddress(omsCompanyAddress);
        return omsOrderReturnApplyAndOmsCompanyAddress;
    }
    
    //批量删除退货申请
    @Override
    public void deletes(Long[] ids) {
        List<Long> id = (List<Long>) Convert.toList(ids);
        this.removeByIds(id);
    }
}
